package com.syaccp.controller;


import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.syaccp.model.SysRole;
import com.syaccp.page.CommonPage;
import com.syaccp.request.PageParamRequest;
import com.syaccp.request.SysRoleRequest;
import com.syaccp.request.SysRoleSearchRequest;
import com.syaccp.response.RoleInfoResponse;
import com.syaccp.service.SysRoleService;
import com.syaccp.utils.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author lixin
 * @since 2023-07-02
 */

@RestController
@RequestMapping("/api/admin/system/role")
@Api(tags = "设置 -- 权限管理 -- 身份管理")
public class RoleController {

	@Resource
	private SysRoleService sysRoleService;

	  /**
     * 分页显示身份管理表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:system:role:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<CommonPage<SysRole>> getList(@Validated SysRoleSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<SysRole> sysRoleCommonPage = CommonPage.restPage(sysRoleService.getList(request, pageParamRequest));
        return Result.success(sysRoleCommonPage);
    }


    /**
     * 新增身份
     * @param systemRoleRequest 新增参数
     */
    @PreAuthorize("hasAuthority('admin:system:role:save')")
    @ApiOperation(value = "新增身份")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<String> save(@RequestBody @Validated SysRoleRequest sysRoleRequest) {
        if (sysRoleService.add(sysRoleRequest)) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 删除身份管理表
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:system:role:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result<String> delete(@RequestParam(value = "id") Integer id) {
        if (sysRoleService.delete(id)) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 修改身份管理表
     * @param systemRoleRequest 修改参数
     */
    @PreAuthorize("hasAuthority('admin:system:role:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result<String> update(@RequestBody @Validated SysRoleRequest sysRoleRequest) {
        if (sysRoleService.edit(sysRoleRequest)) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 查询身份详情
     * @param id String
     */
    //@PreAuthorize("hasAuthority('admin:system:role:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public Result<RoleInfoResponse> info(@PathVariable Integer id) {
        return Result.success(sysRoleService.getInfo(id));
   }

    /**
     * 修改身份状态
     */
    @PreAuthorize("hasAuthority('admin:system:role:update:status')")
    @ApiOperation(value = "修改身份状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
    public Result<String> updateStatus(@Validated @RequestParam(value = "id") Integer id, @Validated @RequestParam(value = "status") Integer status) {
        if (sysRoleService.updateStatus(id, status)) {
            return Result.success("修改成功");
        }
        return Result.failed("修改失败");
    }

}

